'''
@Author: your name
@Date: 2020-02-04 21:05:23
@LastEditTime : 2020-02-05 11:53:54
@LastEditors  : Please set LastEditors
@Description: 生成一个支持鼠标拖拽的窗口
@FilePath: \pyqt5\demo_052_winDrag.py
'''
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('demo_052_winDrag')
        self.resize(500, 500)
        self.setup_ui()

    def setup_ui(self):
        self.m_moveFlag = False

    def mousePressEvent(self,evt):
        self.m_moveFlag = True
        self.press_x = evt.globalX()
        self.press_y = evt.globalY()

        self.origin_x = self.x()
        self.origin_y = self.y()

    def mouseMoveEvent(self, evt):
        if self.m_moveFlag :
            self.m_move_x = evt.globalX() - self.press_x
            self.m_move_y = evt.globalY() - self.press_y
            self.dest_x = self.origin_x+self.m_move_x
            self.dest_y = self.origin_y+self.m_move_y
            self.move(self.dest_x, self.dest_y)
            print(self.m_move_x, self.m_move_y)

    def mouseReleaseEvent(self, evt):
        self.m_moveFlag = False


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Window()
    win.show()

    sys.exit(app.exec_())
